(function(angular) {
	'use strict';
	angular.module("fs.waf")
			.factory("systemConfigService", systemConfigService)
			.filter("translateResKey", [ '$filter', '$locale', '$translate','systemConfigService', function($filter, $locale, $translate, systemConfigService) {
				return function(optionValue, pattern) {
					return (function (optionValue){ 
						var resKey = optionValue.substr(0,optionValue.lastIndexOf("(")).trim();
						var msg = $filter('translate')(resKey);
						  return optionValue.replace(resKey, msg); 
					 	})(optionValue);
				};
			} ]);;
	systemConfigService.$inject = [ '$q', 'loggerFactory', 'commonService',
			'ajaxServiceFactory', '$timeout', 'arithService' ];
	function systemConfigService($q, loggerFactory, commonService,
			ajaxServiceFactory, $timeout, arithService) {
		var log = loggerFactory.getLogger("systemConfigService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getServiceCode : {
				url : 'do/cda/dataList/dynaparmServiceCodeList',
				method : 'post'
			}
		});

		function getGlobServiceCode(ctrl) {
			var defer = $q.defer();
			var serviceCodeOptions = [];
			var params = {
				parmCat : "GLOBSYSTEMCONFIG"
			};
			ajaxService.getServiceCode(params).then(
					function(response) {
						ctrl.system = response;
						if (ctrl.system && ctrl.system.data) {
							var s = [];
							angular.forEach(ctrl.system.data, function(data,
									index, array) {
								if (s.indexOf(data.value) === -1) {
									s.push(data.value);
								}
							});
							if (s.length > 0) {
								for (var i = 0; i < s.length; i++) {
									serviceCodeOptions.push({
										"value" : s[i],
										"label" : s[i]
									});
								}
							}
						}
						ctrl.globServiceCodeOptions = serviceCodeOptions;
						defer.resolve(serviceCodeOptions);
					}, angular.noop);
			return defer.promise;
		}

		function getSiteServiceCode(ctrl) {
			var defer = $q.defer();
			var serviceCodeOptions = [];
			var params = {
				parmCat : "SITESYSTEMCONFIG"
			};
			ajaxService.getServiceCode(params).then(
					function(response) {
						ctrl.system = response;
						if (ctrl.system && ctrl.system.data) {
							var s = [];
							angular.forEach(ctrl.system.data, function(data,
									index, array) {
								if (s.indexOf(data.value) === -1) {
									s.push(data.value);
								}
							});
							if (s.length > 0) {
								for (var i = 0; i < s.length; i++) {
									serviceCodeOptions.push({
										"value" : s[i],
										"label" : s[i]
									});
								}
							}
						}
						ctrl.siteServiceCodeOptions = serviceCodeOptions;
						defer.resolve(serviceCodeOptions);
					}, angular.noop);
			return defer.promise;
		}

		function initGlobDynaParm($scope, ctrl, gridService) {
			var defer = $q.defer();
			ctrl.globDynaParmGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.globList.tableConfigs.table);
			if (ctrl.globDynaParmGridOptions != null) {
				ctrl.globDynaParmGridOptions.data = 'vm.tableData';
				ctrl.globDynaParmGridOptions.enableRowHeaderSelection = false;
				defer.resolve({});
			}
			return defer.promise;
		}
		
		function initSiteDynaParm($scope, ctrl, gridService) {
			var defer = $q.defer();
			ctrl.siteDynaParmGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.siteList.tableConfigs.table);
			if (ctrl.siteDynaParmGridOptions != null) {
				ctrl.siteDynaParmGridOptions.data = 'vm.tableData';
				ctrl.siteDynaParmGridOptions.enableRowHeaderSelection = false;
				defer.resolve({});
			}
			return defer.promise;
		}
		

		function resetGlobProperties(ctrl, serviceCode) {
			var gridConfigs = ctrl.layoutConfig.formConfigs.general.gridConfigs;
			var dynaParm = null;
			if (serviceCode) {
				ctrl.globSystemConfig.serviceCode = serviceCode;
			} else {
				if(ctrl.globServiceCodeOptions){
					if(ctrl.globServiceCodeOptions.length > 0){
					  ctrl.globSystemConfig.serviceCode = ctrl.globServiceCodeOptions[0].value;
					}
				}
			}
			if (ctrl.globSystemConfig.data) {
				ctrl.tableData = [];
				$timeout(function() {
					ctrl.tableData = ctrl.globSystemConfig.data[ctrl.globSystemConfig.serviceCode];
				});
				return;
			} else {
				ctrl.globSystemConfig.data = {};
				angular.forEach(ctrl.globServiceCodeOptions, function(option){
					ctrl.globSystemConfig.data[option.value] = [];
				});
				if (gridConfigs) {
					angular.forEach(gridConfigs,function(gridConfig, key) {
						if (gridConfig.dynamicParameter && gridConfig.dynamicParameter.category == 'GLOBSYSTEMCONFIG') {
							dynaParm = angular.copy(gridConfig);
							if (ctrl.globSystemConfig.dynamicParams) {
								dynaParm.dynamicParams = ctrl.globSystemConfig.dynamicParams;
								
								angular.forEach(dynaParm.dynamicParams, function(dynamicParam){
									dynaParm.dynamicParams[dynamicParam.name] = dynamicParam;
									delete dynaParm.dynamicParams[dynamicParam.serviceCode + '_' + dynamicParam.name];
								});
							}
							ctrl.globSystemConfig.data[gridConfig.dynamicParameter.serviceCode].push(dynaParm);
						}
					});
					sortDynamicParamters(ctrl.globSystemConfig.data[ctrl.globSystemConfig.serviceCode]);
					ctrl.tableData = ctrl.globSystemConfig.data[ctrl.globSystemConfig.serviceCode];
				}
			}
		}

		function resetSiteProperties(ctrl, serviceCode) {
			var gridConfigs = ctrl.layoutConfig.formConfigs.general.gridConfigs;
			var dynaParm = null;
			if (serviceCode) {
				ctrl.siteSystemConfig.serviceCode = serviceCode;
			} else {
				if(ctrl.siteServiceCodeOptions){
					if(ctrl.siteServiceCodeOptions.length > 0){
					  ctrl.siteSystemConfig.serviceCode = ctrl.siteServiceCodeOptions[0].value;
					}
				}
			}
			if (ctrl.siteSystemConfig.data) {
				ctrl.tableData = [];
				$timeout(function() {
					ctrl.tableData = ctrl.siteSystemConfig.data[ctrl.siteSystemConfig.serviceCode];
				});
				return;
			} else {
				ctrl.siteSystemConfig.data = {};
				angular.forEach(ctrl.siteServiceCodeOptions, function(option){
					ctrl.siteSystemConfig.data[option.value] = [];
				});
				if (gridConfigs) {
					angular.forEach(gridConfigs,function(gridConfig, key) {
						if (gridConfig.dynamicParameter && gridConfig.dynamicParameter.category == 'SITESYSTEMCONFIG') {
							dynaParm = angular.copy(gridConfig);
							if (ctrl.siteSystemConfig.dynamicParams) {
								dynaParm.dynamicParams = ctrl.siteSystemConfig.dynamicParams;
								angular.forEach(dynaParm.dynamicParams, function(dynamicParam){
									dynaParm.dynamicParams[dynamicParam.name] = dynamicParam;
									delete dynaParm.dynamicParams[dynamicParam.serviceCode + '_' + dynamicParam.name];
								});
							}
							ctrl.siteSystemConfig.data[gridConfig.dynamicParameter.serviceCode].push(dynaParm);
						}
					});
					sortDynamicParamters(ctrl.siteSystemConfig.data[ctrl.siteSystemConfig.serviceCode]);
					ctrl.tableData = ctrl.siteSystemConfig.data[ctrl.siteSystemConfig.serviceCode];
				}
			}
		}
		
		
		function getGlobDynaparmTwo(ctrl, globSystemConfig) {
			resetGlobProperties(ctrl,
					globSystemConfig ? globSystemConfig.serviceCode : undefined);
		}

		
		function getSiteDynaparmTwo(ctrl, siteSystemConfig) {
			resetSiteProperties(ctrl,
					siteSystemConfig ? siteSystemConfig.serviceCode : undefined);
		}
		
		function sortDynamicParamters(serviceDymamicParamters){
			if(!serviceDymamicParamters){
				return;
			}
			serviceDymamicParamters.sort(function(p1, p2){
				return arithService.gt(p1.dynamicParameter.dispOrder, p2.dynamicParameter.dispOrder) ? 1 : -1;
			});
		}
		return {
			getGlobServiceCode : getGlobServiceCode,
			getSiteServiceCode : getSiteServiceCode,
			initGlobDynaParm : initGlobDynaParm,
			getGlobDynaparmTwo : getGlobDynaparmTwo,
			resetGlobProperties : resetGlobProperties,
			initSiteDynaParm : initSiteDynaParm,
			getSiteDynaparmTwo : getSiteDynaparmTwo,
			resetSiteProperties : resetSiteProperties
		};
	}
})(angular);